/**
 * 
 */
package org.paybiz.core.payplatform.tenPay.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @Title: AutoCheckOrderHandler.java
 * @Description: 自动对账处理类
 * @author Nassir.wen
 * @date 2010-11-11 下午02:01:16
 * @version V1.0
 * @Company: MSD.
 * @Copyright Copyright (c) 2010
 */
public class AutoCheckOrderHandler {

	/** 请求的参数 */
	private SortedMap parameters;
	/** 密钥 */
	private String key = "";
	/** 编码 */
	private String chartset = "GB2312";
	/** debug信息*/
	private String debugInfo = "";
	/** 请求链接*/
	private String gateUrl = "http://mch.tenpay.com/cgi-bin/cfbi_query_order_v3.cgi";

	public AutoCheckOrderHandler() {
		parameters = new TreeMap();
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getChartset() {
		return chartset;
	}

	public void setChartset(String chartset) {
		this.chartset = chartset;
	}

	public String getGateUrl() {
		return gateUrl;
	}

	public void setGateUrl(String gateUrl) {
		this.gateUrl = gateUrl;
	}

	public String getDebugInfo() {
		return debugInfo;
	}

	/**
	 * 设置参数值
	 * 
	 * @param parameter
	 *            参数名称
	 * @param parameterValue
	 *            参数值
	 */
	public void setParameter(String parameter, String parameterValue) {
		String v = "";
		if (null != parameterValue) {
			v = parameterValue.trim();
		}
		this.parameters.put(parameter, v);
	}

	/**
	 * 获取带参数的请求URL
	 * 
	 * @return String
	 * @throws UnsupportedEncodingException
	 */
	public String getRequestURL() throws UnsupportedEncodingException {

		this.createSign();

		StringBuffer sb = new StringBuffer();
		Set es = this.parameters.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			sb.append(k + "=" + URLEncoder.encode(v, chartset) + "&");
		}

		// 去掉最后一个&
		String reqPars = sb.substring(0, sb.lastIndexOf("&"));

		return this.getGateUrl() + "?" + reqPars;

	}

	/**
	 * 创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
	 */
	protected void createSign() {
		StringBuffer sb = new StringBuffer();
		Set es = this.parameters.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			if (null != v && !"".equals(v) && !"sign".equals(k)
					&& !"key".equals(k)) {
				sb.append(k + "=" + v + "&");
			}
		}
		sb.append("key=" + this.getKey());

		String sign = MD5Util.MD5Encode(sb.toString(), chartset).toLowerCase();

		this.setParameter("sign", sign);

		// debug信息
		this.setDebugInfo(sb.toString() + " => sign:" + sign);

	}

	/**
	 *设置debug信息
	 */
	protected void setDebugInfo(String debugInfo) {
		this.debugInfo = debugInfo;
	}
}
